<include file="Public/header"/>
<include file="Public/menu"/>
<style type="text/css">
.goods-table-view{width:100%;overflow:auto;}
.goods-table-view th{white-space:nowrap;}
</style>
<!-- 内容页 -->
<div class="content-wrapper">
    <!-- 面包屑 -->
    <section class="content-header">
        <h1>
            商品
            <small>管理</small>
        </h1>
        <ol class="breadcrumb">
            <li><a href="#"><i class="fa fa-dashboard"></i> 商品管理</a></li>
            <li class="active">上架商品管理</li>
        </ol>
    </section>
    <!-- 内容 -->
    <section class="content">
        <div class="row">
            <div class="col-md-12">
                <div class="box" style="overflow-x: scroll">
                    <div class="box-header">
                        <h3 class="box-title"></h3>


                        <div class="form-inline pull-right">
                            <form action="" method="get">
                                <input id="p" name="p"   type="hidden" value="1" />
                                <fieldset>
                                    <div class="input-group input-group-sm">
                                        <select class="form-control valid" name="cat_id" style="height: 30px; font-size: 12px;" aria-invalid="false">
                                            <option value="">请选择商品分类</option>
                                            <foreach name="parent" item="v">
                                                <option value="{$v.store_cat_id}">{$v.cat_name}</option>
                                                <foreach name="second" item="s">
                                                    <if condition="$s['parent_id'] eq $v['store_cat_id']">
                                                    <option value="{$s.store_cat_id}">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{$s.cat_name}</option>
                                                    </if>
                                                </foreach>
                                            </foreach>
                                        </select>
                                    </div>
                                    <div class="input-group input-group-sm">
                                        <span class="input-group-addon"><strong>货品条码</strong></span>
                                        <input type="text" class="form-control" placeholder="货品条码" name="barcode" value="{$barcode}">
                                    </div>
                                    <div class="input-group input-group-sm">
                                        <span class="input-group-addon"><strong>货品名称</strong></span>
                                        <input type="text" class="form-control" placeholder="货品名称" name="name" value="{$name}">
                                    </div>
                                    <div class="input-group input-group-sm">
                                        <div class="input-group-btn">
                                            <button type="submit" class="btn btn-primary"><i class="fa fa-search"></i>
                                            </button>
                                        </div>
                                    </div>
                                    <div class="input-group input-group-sm">
                                        <a href="/admin/StoreGoods/excel">下载Excel</a>
                                    </div>
                                </fieldset>
                            </form>
                        </div>
                    </div>
                    <!-- /.box-header -->
                    <div class="box-body table-responsive no-padding" style="height:480px;overflow:auto;">
                        <table class="table table-hover goods-table-view">
                            <tbody>
                            <tr>
                                <th><input type="checkbox" class="grid-select-all"></th>
                                <th>ID</th>
                                <th>条码</th>
                                <th>货品名称</th>
                                <th>活动中</th>
                                <th>图片</th>
                                <th>指导价</th>
                                <th>货品描述</th>
                                <th>
                                    <div class="btn btn-sm btn-success"><i class="fa fa-save"></i>&nbsp;&nbsp;标签</div>
                                </th>
                                <th>店铺市场价</th>
                                <th>店铺销售价</th>
                                <th>店铺库存</th>
                                <th>货号</th>
                                <th>是否快递</th>
                                <th>冻结库存</th>
                                <th>商品分类</th>
                                <th>特别说明</th>
                            </tr>
                            <foreach name="product_relation_msg" item="v">
                                <tr class="cg-product-tr yc-srarea">
                                    <if condition="$v['store_status'] == 'true'">
                                        <td><input type="checkbox" class="grid-item" data-id="{$v.product_id}" checked>
                                        </td>
                                        <else/>
                                        <td><input type="checkbox" class="grid-item" data-id="{$v.product_id}"></td>
                                    </if>
                                    <td>{$v.product_id}</td>
                                    <td>{$v.barcode}</td>
                                    <td>{$v.name}</td>
                                    <td>
                                        <?php
                                             if (goods_is_Order_promotion($v['store_pid'], $v['local_id'])) {
                                                echo '<span class="badge bg-green">订单促销</span>';
                                        }
                                        if (goods_is_Goods_promotion($v['store_pid'], $v['local_id'])) {
                                        echo '<span class="badge bg-green">商品促销</span>';
                                        }
                                        if (goods_is_shangou($v['store_pid'], $v['local_id'])) {
                                        echo '<span class="badge bg-green">闪购</span>';
                                        }

                                        if (checkProductIsGroupPromotion($v['store_pid'], $v['local_id'])) {
                                        echo '<span class="badge bg-green">组合促销</span>';
                                        }
                                        if (checkProductIsSpecial($v['store_pid'], $v['local_id'])) {
                                        echo '<span class="badge bg-green">专题商品包</span>';
                                        }
                                        ?>
                                    </td>
                                    <td><img class="scale-img" src="{:getThumb($v['goods_id'])}" data-bimg="{:getThumb($v['goods_id'])}"/></td>
                                    <td><span class="badge bg-green">{$v.guidance_price}</span></td>
                                    <td>{$v.spec_info}</td>
                                    <td><a href="#" class="btn btn-sm btn-success labels" id="{$v.product_id}"><i
                                            class="fa fa-save"></i>&nbsp;&nbsp;标签</a></td>

                                    <php>$marketprice = get_store_product($v['product_id'], 'marketprice')</php>
                                    <td><input type="text" class="form-control grid-mktprice" name="mktprice" value="{$marketprice}" style="width:100px;height:30px;"></td>
                                    <php>$price = get_store_product($v['product_id'], 'price')</php>
                                    <td><input type="text" class="form-control grid-price" name="price" value="{$price}" style="width:100px;height:30px;"></td>
                                    <php>$store = get_store_product($v['product_id'], 'store')</php>
                                    <php>$freez = get_store_product($v['product_id'], 'freez')</php>
                                    <td><input type="text" class="form-control grid-store" name="store" data-min="{$freez}" value="{$store}" style="width:100px;height:30px;"></td>
                                    <!--<php>$discount = get_store_product($v['product_id'], 'discount')</php>-->
                                    <!--<td><input type="text" class="form-control grid-discount" name="discount"-->
                                    <!--value="<if condition= '!empty($discount)'>{$discount}<else />{$v.discount}</if>" style="width: 100px;height: 30px;"></td>-->
                                    <php>$bn = get_store_product($v['product_id'], 'bn')</php>
                                    <td><input type="text" class="form-control grid-bn" name="bn" value="{$bn}" style="width:100px;height:30px;"></td>
                                    <php>$is_transport = get_store_product($v['product_id'], 'is_transport')</php>
                                    <td>
                                        <select name="is_transport" class="form-control grid-transport"style="min-width:62px" >
                                            <option value="true" <if condition= "$is_transport == 'true'">selected</if>>是</option>
                                            <option value="false" <if condition= "$is_transport == 'false'">selected</if>>否</option>
                                        </select>
                                    </td>                                    
                                    <td>{$freez}</td>
                                    <td>
                                        <select class="form-control grid-cat" style="min-width:100px;">
                                            <option value="">请选择</option>
                                            <?php
                                            $goods_cat = get_store_product($v['product_id'], 'scate_id');
                                            foreach($parent as $p){
                                            ?>
                                            <option
                                            <?php echo ($p['store_cat_id'] == $goods_cat) ? 'selected' : ''; ?>
                                            value="<?php echo $p['store_cat_id'];?>"><?php echo $p['cat_name'];?></option>

                                            <?php foreach($second as $s){ if ($s['parent_id'] == $p['store_cat_id']) { ?>


                                            <option
                                            <?php echo ($s['store_cat_id'] == $goods_cat) ? 'selected' : ''; ?>
                                            value="<?php echo $s['store_cat_id'];?>">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $s['cat_name'];?></option>
                                            <?php foreach($three as $t){ if ($t['parent_id'] == $s['store_cat_id']) { ?>
                                            <option
                                            <?php echo ($t['store_cat_id'] == $goods_cat) ? 'selected' : ''; ?>
                                            value="<?php echo $t['store_cat_id'];?>">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $t['cat_name'];?></option>

                                            <?php }}  ?>

                                            <?php }}  ?>
                                            <?php } ?>
                                        </select>
                                    </td>
                                    <td><input type="text" name="explain" value="{$v.explain}" class="form-control grid-explain" style="width:120px;"></td>
                                </tr>
                                <tr class="yc-validation">
                                    <td>&nbsp;</td>
                                    <td>&nbsp;</td>
                                    <td>&nbsp;</td>
                                    <td>&nbsp;</td>
                                    <td>&nbsp;</td>
                                    <td>&nbsp;</td>
                                    <td>&nbsp;</td>
                                    <td>&nbsp;</td>
                                    <td>&nbsp;</td>
                                    <td><span class="valid-mktprice"></span></td>
                                    <td><span class="valid-price"></span></td>
                                    <td><span class="valid-store"></span></td>
                                    <td><span class="valid-bn"></span></td>
                                    <td>&nbsp;</td>
                                    <td></td>
                                    <td><span class="valid-cat"></span></td>
                                    <td>&nbsp;</td>
                                </tr>
                            </foreach>
                            </tbody>
                        </table>
                    </div>
                    <div class="box-footer clearfix">
                        <input type="checkbox" class="grid-select-all">&nbsp;&nbsp;&nbsp;
                        <a class="btn btn-sm btn-info add-goods">保存</a>
                        {$show}
                    </div>
                    <!-- /.box-body -->
                </div>
            </div>
        </div>
    </section>
</div>
<!-- 底部 -->
<include file="Public/common_footer"/>
<script src="/Public/layer/layer.js"></script>
<script type="text/javascript">	
	var yc_srareaArr = [];
	var yc_validateArr = [];
	var yc_choseArr = [];
	var _isAjaxSave = false;
	$(".yc-srarea").each(function(){
		yc_srareaArr.push($(this));
		yc_choseArr.push(true);
	});
	$(".yc-validation").each(function(){
		yc_validateArr.push($(this));
	});
	// 检查保存数据
	function checkSaveDatas(){				
		$(".grid-item").each(function(i, o){
			if($(o).prop("checked")){
				yc_choseArr[i] = true;
			}else{
				yc_choseArr[i] = false;
			}			
		});		
		$(".grid-mktprice").each(function(i, o){
			var v = $(o).val();
			var cur = yc_validateArr[i].find(".valid-mktprice");
			cur.removeClass("error");
			if(yc_choseArr[i]){
				if(mbValidator.isEmpty(v)){
                    //市场价格为空可以通过验证
                    cur.html("");
                    //cur.addClass("error").html("请输入店铺市场价");
				}else if(!mbValidator.isGoodsPrice(v)){
					cur.addClass("error").html("请输入大于0的数字");
				}else{
					mbValidator.fixPrice(o, v);
					cur.html("");
				}
			}
		});
		$(".grid-price").each(function(i, o){
			var v = $(o).val();
			var cur = yc_validateArr[i].find(".valid-price");
			cur.removeClass("error");
			if(yc_choseArr[i]){
				if(mbValidator.isEmpty(v)){
					cur.addClass("error").html("请输入店铺销售价");
				}else if(!mbValidator.isGoodsPrice(v)){
					cur.addClass("error").html("请输入大于0的数字");
				}else{
					mbValidator.fixPrice(o, v);
					cur.html("");
				}	
			}
			
		});
		$(".grid-store").each(function(i, o){
			var v = $(o).val();
			var cur = yc_validateArr[i].find(".valid-store");
			cur.removeClass("error");
			if(yc_choseArr[i]){
				if(mbValidator.isEmpty(v)){
					cur.addClass("error").html("请输入店铺库存");
				}else if(!mbValidator.isPositiveInt(v)){
					cur.addClass("error").html("店铺库存数量必须是大于0的整数");
				}else if(parseInt(v) < parseInt($(o).data("min"))){
					cur.addClass("error").html("店铺库存数量要大于冻结库存数量"+$(o).data("min"));
				}else{
					cur.html("");
				}
			}
		});
		$(".grid-bn").each(function(i, o){
			var v = $(o).val();
			var cur = yc_validateArr[i].find(".valid-bn");
			cur.removeClass("error");
			if(yc_choseArr[i]){
				if(mbValidator.isEmpty(v)){
					cur.addClass("error").html("请输入货号");
				}else{
					$(o).val(v.trim());
					cur.html("");
				}
			}			
		});
		$(".grid-cat").each(function(i, o){
			var v = $(o).val();
			var cur = yc_validateArr[i].find(".valid-cat");
			cur.removeClass("error");
			if(yc_choseArr[i]){
				if(mbValidator.isEmpty(v)){
					cur.addClass("error").html("请选择商品分类");
				}else{
					cur.html("");
				}
			}			
		});
	}
    $(function () {
		showBigPicture();
        $('.grid-select-all').change(function () {
            if (this.checked) {
                $('.grid-item').prop("checked", true);
            } else {
                $('.grid-item').prop("checked", false);
            }
        });
        $('.add-goods').on('click', function () {
			if(_isAjaxSave) return;
			_isAjaxSave = true; 	
			checkSaveDatas();
			if($(".error").length > 0){
				_isAjaxSave = false;
				alert("有错误数据，请根据提示修改后再保存！");
				return;
			}
            var selected = [];

            $('.grid-item').each(function (i, o) {
                var id = $(this).data('id');
                var mktprice = yc_srareaArr[i].find('.grid-mktprice').val();
                var price = yc_srareaArr[i].find('.grid-price').val();
                var store = yc_srareaArr[i].find('.grid-store').val();
//              var discount = $(this).parent().nextAll().find('.grid-discount').val();
                var discount = '';
                var cat = yc_srareaArr[i].find('.grid-cat').val();
                var bn = yc_srareaArr[i].find('.grid-bn').val();
                var transport = yc_srareaArr[i].find('.grid-transport').val();
                var explain = yc_srareaArr[i].find('.grid-explain').val();
                bn = encodeURIComponent(bn);
                if ($(this).is(':checked')) {
                    var goods_values = id + '#' + mktprice + '#' + price + '#' + store + '#' + discount + '#1' + '#' + cat + '#' + bn + '#' + transport + '#' + explain;
                    selected.push(goods_values);
                } else {
                    var goods_values = id + '#' + mktprice + '#' + price + '#' + store + '#' + discount + '#0' + '#' + cat + '#' + bn + '#' + transport + '#' + explain;
                    selected.push(goods_values);
                }
            });

            if (selected.length == 0) {
				_isAjaxSave = false;
                return;
            }
			GPopup.loadDialog();
			 $.post('/admin/StoreGoods/addGoods/', {data: selected}, function (data) {
				 GPopup.removeLoad();
				 _isAjaxSave = false;
                 data = eval("("+data+")");
                 GPopup.alertTip(data.msg);
//				 console.log(data);
//				 if(data.status == 'true') {
//					GPopup.alertTip('保存成功！');
//					setTimeout(function(){window.location.reload();}, 2000);
//				}else{
//					GPopup.alertTip(data.msg);
//				}
            });
        });

        $('.labels').click(function () {
            var id = $(this).attr('id');
            layer.ready(function () {
                layer.open({
                    type: 2,
                    title: '标签',
                    fix: false,
                    maxmin: true,
                    shadeClose: true,
                    area: ['600px', '500px'],
                    content: "/admin/storeGoods/label/id/" + id,
                    end: function () {
                        layer.tips('Hi', '#about', {tips: 1})
                    }
                });
            });
        })
    });
</script>
